package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsIssueMapper;
import com.qf.pojo.DtsIssue;
import com.qf.pojo.DtsOrder;
import com.qf.service.DtsAdminIssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class DtsAdminIssueServiceImpl implements DtsAdminIssueService {

    @Autowired
    DtsIssueMapper issueMapper;

    @Override
    public IPage<DtsIssue> queryPage(Integer page, Integer limit, String sort, String order, String question) {
        IPage<DtsIssue> iPage = new Page<>(page,limit);
        QueryWrapper<DtsIssue> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(question)){
            qw.like("`question`",question);
        }
        qw.eq("deleted",false);
        if(!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }
        issueMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public void update(DtsIssue issue) {
        issue.setUpdateTime(new Date());
        issueMapper.updateById(issue);
    }

    @Override
    public DtsIssue insert(DtsIssue issue) {
        issue.setAddTime(new Date());
        issue.setUpdateTime(new Date());
        issue.setDeleted(false);
        issueMapper.insert(issue);
        return issue;
    }

    @Override
    public void delete(DtsIssue issue) {
        issue.setDeleted(true);
        issueMapper.updateById(issue);
    }
}
